/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Ajax;

import DTO.DatVeDTO;
import DTO.DoiTuongDTO;
import DTO.Ghe;
import DTO.GiuongDTO;
import DTO.LoaiTau;
import DTO.NguoiDung;
import DTO.Tau;
import DTO.UserList;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Tu Nguyen
 */
@WebServlet(name = "DatVe", urlPatterns = {"/DatVe"})
public class DatVe extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        
        
       
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        
        
        
        
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        
         try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
              HttpSession ss = request.getSession();     
              if(ss.getAttribute("TTDatVe")==null)
              {
                  ArrayList<DatVeDTO> lstDV = new ArrayList<DatVeDTO>();
                 ss.setAttribute("TTDatVe", lstDV);
              }
              UserList ul = new UserList();
              ul = (UserList)ss.getAttribute("SSUser");
              NguoiDung nd = BUS.NguoiDungBUS.Get(ul.Id);
              
              String gaDi = request.getParameter("gaDi");
              String gaDen = request.getParameter("gaDen");
              String ghe = request.getParameter("ghe");
              String chuyen = request.getParameter("chuyen");
              String toa = request.getParameter("toa");
              String sht = request.getParameter("sohieutoa");
              String doiTuong =request.getParameter("doiTuong");
              String tauId = request.getParameter("tau");
              ArrayList<DatVeDTO> tmp = new ArrayList<DatVeDTO>();
              tmp = (ArrayList<DatVeDTO>) ss.getAttribute("TTDatVe");
                DatVeDTO d = new DatVeDTO();
                DateFormat dateformat = new SimpleDateFormat("ddMMyyyyhhmmss");
                Date date = new Date();
                d.id = dateformat.format(date);
                DateFormat dateformat1 = new SimpleDateFormat("yyyy-MM-dd");
                d.ngaydat  = dateformat1.format(date);
                d.chuyen = chuyen;
                d.toa= sht;
                d.ga_di = Integer.parseInt(gaDi);
                d.ga_den = Integer.parseInt(gaDen);
                if(toa.equals("1"))
                    d.loai_ghe = "Ghế thường";
                else 
                    d.loai_ghe = "Giường nằm";
                    
                d.ghe = ghe;
                d.tinh_trang = "Chua Giao";
                d.doi_tuong  = Integer.parseInt(doiTuong);
                d.cmnd = nd.CMND;
                // lay gia ghe vua click
                Tau tau = BUS.TauBUS.Get(tauId);
                LoaiTau lt = BUS.LoaiTauBUS.Get(tau.LoaiTau);
                DoiTuongDTO dt = new DoiTuongDTO();
                dt  = BUS.DoiTuongBUS.Get(d.doi_tuong);
                if(toa.equals("1"))
                {
                    Ghe g = new Ghe();
                    g = BUS.GheBUS.Get(ghe, sht);
                    d.giave = g.Gia + (g.Gia*lt.Tang_Gia/100) - (g.Gia*dt.giamgia/100);
                    out.println("Giá vé :" + g.Gia);
                }
                else
                {
                    String phong = request.getParameter("phong");
                    GiuongDTO gi = BUS.GiuongBUS.Get(d.ghe, phong);
                     d.giave = gi.gia + (gi.gia*lt.Tang_Gia/100) -(gi.gia*dt.giamgia/100);
                    out.println("Giá vé :" + gi.gia);
                }
                // cap nhat lai ghe co su thay doi doi tuong
                int index = -1;
                for(DatVeDTO dv : tmp)
                {
                    if(dv.ghe.equals(d.ghe)&& dv.ga_di==d.ga_di & dv.ga_den == d.ga_den && dv.chuyen.equals(d.chuyen))
                        index = tmp.indexOf(dv);
                }
                if(index >=0)
                    tmp.remove(index);
                    
                tmp.add(d);
                ss.setAttribute("TTDatVe", tmp);    
                
              
        }
        
        
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
